import axios, { type AxiosRequestConfig } from "axios"
import { createDiscreteApi } from 'naive-ui'

const { message } = createDiscreteApi(['message']);

const service = axios.create({
    timeout: 20000,
    baseURL: ''
})

service.interceptors.request.use(config => {
    config.headers.testSign = 'test'
    return config
})

service.interceptors.response.use(response => {
    console.log(response, '返回的信息')
    return response.data
}, error => {
    if (error) {
        message.error('请求错误')
    }
    return Promise.reject(error)
})

const request = async function (config: AxiosRequestConfig): Promise<[any, any]> {
    try {
        const res = await service.request(config);
        return [null, res]
    } catch (err) {
        return [err, null]
    }
}

export default request;